package com.liqi.web.controller.deployment;

import com.liqi.common.annotation.Log;
import com.liqi.common.core.controller.BaseController;
import com.liqi.common.core.domain.AjaxResult;
import com.liqi.common.core.page.TableDataInfo;
import com.liqi.common.enums.BusinessType;
import com.liqi.common.utils.DateUtils;
import com.liqi.common.utils.StringUtils;
import com.liqi.common.utils.poi.ExcelUtil;
import com.liqi.common.utils.uuid.GenerateCodeUtils;
import com.liqi.deployment.domain.DvDeployment;
import com.liqi.deployment.domain.DvDeploymentModule;
import com.liqi.deployment.domain.DvOperationUser;
import com.liqi.deployment.service.IDvDeploymentModuleService;
import com.liqi.deployment.service.IDvDeploymentService;
import com.liqi.device.domain.DvDevice;
import com.liqi.device.domain.DvDeviceModule;
import com.liqi.device.service.*;
import com.liqi.system.service.ISysDeptService;
import com.liqi.system.service.ISysRegionService;
import com.liqi.system.service.device.IDvOperationUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备部署管理Controller
 *
 * @author xiaoli
 * @date 2022-02-14
 */
@Api(tags = "设备部署管理")
@RestController
@RequestMapping("/device/placeManager")
public class DvDeploymentController extends BaseController
{
    @Autowired
    private IDvDeploymentService dvDeploymentService;

    @Autowired
    private IDvDeviceService dvDeviceService;

    @Autowired
    private ISysRegionService sysRegionService;

    @Autowired
    private IDvOperationUserService dvOperationUserService;

    @Autowired
    private IDvBrandService dvBrandService;

    @Autowired
    private IDvManufacturerService dvManufacturerService;

    @Autowired
    private IDvDeploymentModuleService dvDeploymentModuleService;

    @Autowired
    private IDvDeviceModuleService dvDeviceModuleService;

    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询设备部署管理列表
     */
    @ApiOperation("查询设备部署管理列表")
    @PreAuthorize("@ss.hasPermi('device:placeManager:list')")
    @GetMapping("/list")
    public TableDataInfo list(DvDeployment dvDeployment)
    {
        startPage();
        List<DvDeployment> list = dvDeploymentService.selectDvDeploymentList(dvDeployment);
        return getDataTable(list);
    }
    /**
     * 查询设备部署管理列表
     */
    @ApiOperation("查询设备部署管理列表")
    @GetMapping("/listJiankong")
    public TableDataInfo listJiankong(DvDeployment dvDeployment)
    {
        startPage();
        List<DvDeployment> list = dvDeploymentService.selectDvDeploymentJiankongList(dvDeployment);
        return getDataTable(list);
    }

    /**
     * 查询设备部署管理列表 给设备日志新增使用
     */
    /*@PreAuthorize("@ss.hasPermi('device:placeManager:listForLog')")*/
    @ApiOperation("给设备日志新增使用的查询设备部署管理列表")
    @GetMapping("/listForLog")
    public TableDataInfo listForLog(DvDeployment dvDeployment)
    {
        startPage();
        List<DvDeployment> list = dvDeploymentService.selectDvDeploymentList(dvDeployment);
        return getDataTable(list);
    }

    /**
     * 导出设备部署管理列表
     */
    @ApiOperation("导出设备部署管理列表")
    @PreAuthorize("@ss.hasPermi('device:placeManager:export')")
    @Log(title = "设备部署管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DvDeployment dvDeployment)
    {
        List<DvDeployment> list = dvDeploymentService.selectDvDeploymentList(dvDeployment);
        ExcelUtil<DvDeployment> util = new ExcelUtil<DvDeployment>(DvDeployment.class);
        util.exportExcel(response, list, "设备部署管理数据");
    }

    /**
     * 获取设备部署管理详细信息
     */
    @ApiOperation("获取设备部署管理详细信息")
    @PreAuthorize("@ss.hasPermi('device:placeManager:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(dvDeploymentService.selectDvDeploymentById(id));
    }

    /**
     * 新增设备部署管理
     */
    @ApiOperation("新增设备部署管理")
    @PreAuthorize("@ss.hasPermi('device:placeManager:add')")
    @Log(title = "设备部署管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DvDeployment dvDeployment)
    {
        dvDeployment.setDeploymentCode(GenerateCodeUtils.getSequnce());
        dvDeployment.setCreateBy(getUsername());
        dvDeployment.setCreateUserId(getUserId().toString());
        dvDeployment.setCreateTime(DateUtils.getNowDate());
        dvDeployment.setDeptId(getDeptId().toString());
        dvDeployment.setStatus(0);
        dvDeployment.setUpdateBy(getUsername());
        dvDeployment.setUpdateTime(DateUtils.getNowDate());
        String deviceCode = dvDeployment.getDeviceCode();
        DvDevice dvDevice = dvDeviceService.selectDvDeviceByCode(deviceCode);
        List<DvDeviceModule> dvDeviceModules = dvDeviceModuleService.selectDvDeviceModuleListByDeviceCode(deviceCode);
        for (DvDeviceModule dvDeviceModule : dvDeviceModules) {
            DvDeploymentModule DvDeploymentModule = new DvDeploymentModule();
            DvDeploymentModule.setDeviceId(dvDeployment.getDeviceId());
            DvDeploymentModule.setDeviceName(dvDevice.getName());
            DvDeploymentModule.setDeviceCode(dvDeployment.getDeviceCode());
            DvDeploymentModule.setModelCode(dvDeviceModule.getModuleCode());
            DvDeploymentModule.setModelName(dvDeviceModule.getModuleName());
            DvDeploymentModule.setModelStatus(0);
            DvDeploymentModule.setCreateBy(getUsername());
            DvDeploymentModule.setUpdateBy(getUsername());
            DvDeploymentModule.setCreateTime(DateUtils.getNowDate());
            DvDeploymentModule.setUpdateTime(DateUtils.getNowDate());
            DvDeploymentModule.setStatus(0);
            dvDeploymentModuleService.insertDvDeploymentModule(DvDeploymentModule);
        }
        String operationUserName = dvDeployment.getOperationUserName();
        String phoneNumber = dvDeployment.getPhoneNumber();
        if (StringUtils.isNotBlank(operationUserName) && StringUtils.isNotBlank(phoneNumber)) {
            DvOperationUser dvOperationUser = new DvOperationUser();
            dvOperationUser.setOperationUserName(operationUserName);
            dvOperationUser.setPhoneNumber(phoneNumber);
            List<DvOperationUser> dvOperationUsers = dvOperationUserService.selectDvOperationUserByNameOrPhone(dvOperationUser);
            if (dvOperationUsers.size() == 0) {
                dvOperationUser.setOperationUserCode(GenerateCodeUtils.getSequnce());
                dvOperationUser.setSex("0");
                dvOperationUser.setCreateBy(getUsername());
                dvOperationUser.setCreateTime(DateUtils.getNowDate());
                dvOperationUser.setUpdateBy(getUsername());
                dvOperationUser.setUpdateTime(DateUtils.getNowDate());
                dvOperationUser.setDeptId(getDeptId().toString());
                dvOperationUser.setCreateUserId(getUserId().toString());
                dvOperationUserService.insertDvOperationUser(dvOperationUser);
            }
        }
        return toAjax(dvDeploymentService.insertDvDeployment(dvDeployment));
    }
    /*public AjaxResult add(@RequestBody DvDeployment dvDeployment)
    {
        String deviceCode = dvDeployment.getDeviceCode();
        DvDevice dvDevice = dvDeviceService.selectDvDeviceByCode(deviceCode);
        List<DvDeviceModule> dvDeviceModules = dvDeviceModuleService.selectDvDeviceModuleListByDeviceCode(deviceCode);
        for (DvDeviceModule dvDeviceModule : dvDeviceModules) {
            DvDeploymentModule DvDeploymentModule = new DvDeploymentModule();
            DvDeploymentModule.setDeploymentCode(dvDeployment.getDeploymentCode());
            DvDeploymentModule.setDeviceName(dvDevice.getName());
            DvDeploymentModule.setDeviceCode(dvDeployment.getDeviceCode());
            DvDeploymentModule.setModelCode(dvDeviceModule.getModuleCode());
            DvDeploymentModule.setModelName(dvDeviceModule.getModuleName());
            DvDeploymentModule.setModelStatus(0);
            DvDeploymentModule.setCreateBy(getUsername());
            DvDeploymentModule.setUpdateBy(getUsername());
            DvDeploymentModule.setCreateTime(DateUtils.getNowDate());
            DvDeploymentModule.setUpdateTime(DateUtils.getNowDate());
            DvDeploymentModuleService.insertDvDeploymentModule(DvDeploymentModule);
        }
        dvDeployment.setCreateBy(getUsername());
        dvDeployment.setCreateUserId(getUserId().toString());
        dvDeployment.setCreateTime(DateUtils.getNowDate());
        dvDeployment.setDeptId(getDeptId().toString());
        dvDeployment.setUpdateBy(getUsername());
        dvDeployment.setUpdateTime(DateUtils.getNowDate());
        dvDeployment.setStatus(0);
        return toAjax(dvDeploymentService.insertDvDeployment(dvDeployment));
    }*/

    /**
     * 修改设备部署管理
     */
    @ApiOperation("修改设备部署管理")
    @PreAuthorize("@ss.hasPermi('device:placeManager:edit')")
    @Log(title = "设备部署管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DvDeployment dvDeployment)
    {
        dvDeployment.setDeptId(getDeptId().toString());
        dvDeployment.setUpdateBy(getUsername());
        dvDeployment.setUpdateTime(DateUtils.getNowDate());
        return toAjax(dvDeploymentService.updateDvDeployment(dvDeployment));
    }

    /**
     * 删除设备部署管理
     */
    @ApiOperation("删除设备部署管理")
    @PreAuthorize("@ss.hasPermi('device:placeManager:remove')")
    @Log(title = "设备部署管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @Transactional
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        int i = 0;
        for (long id : ids) {
            DvDeployment dvDeployment = dvDeploymentService.selectDvDeploymentById(id);
            if (dvDeployment != null ) {
                dvDeployment.setDeptId(getDeptId().toString());
                dvDeployment.setUpdateBy(getUsername());
                dvDeployment.setUpdateTime(DateUtils.getNowDate());
                dvDeployment.setStatus(1);
                i = dvDeploymentService.updateDvDeployment(dvDeployment);
            }
        }
        return toAjax(i);
    }

}
